/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package uit.tkorg.coauthorgraph.servlet.graph;

import edu.uci.ics.jung.graph.UndirectedSparseGraph;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import uit.tkorg.coauthorgraph.bo.CoAuthorGraphBO;
import uit.tkorg.coauthorgraph.bo.ConferenceBO;
import uit.tkorg.coauthorgraph.bo.JournalBO;
import uit.tkorg.coauthorgraph.graph.CoAuthorGraphVertex;
import uit.tkorg.coauthorgraph.utility.HashMapUtility;

/**
 *
 * @author Tin Huynh
 */
public class CalculateTotalConfPercentWithCoAuthorServlet extends HttpServlet {

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         try {
            System.out.println("************************************************************");
            System.out.println("------------Calculate Metrics: #3. The Total Conference Percent with Co-Author -------");
            HttpSession session = request.getSession();
            UndirectedSparseGraph graph = (UndirectedSparseGraph) session.getAttribute("coAuthorGraph");
            ArrayList initialAuthorIDList = (ArrayList) session.getAttribute("initialAuthorIDList");
            CoAuthorGraphBO coAuthorGraphBO = new CoAuthorGraphBO();
            ArrayList authorVertexIDList = coAuthorGraphBO.getAuthorVertexIDListInGraph(graph);

            ConferenceBO confBO = new ConferenceBO();
            HashMap confPaperPercentWithCoAuthorHashMap = confBO.calculateTotalConfPercentWithCoAuthor(authorVertexIDList,
                                                                                                initialAuthorIDList);
            confPaperPercentWithCoAuthorHashMap = HashMapUtility.getSortedMapDescending(confPaperPercentWithCoAuthorHashMap);
            request.setAttribute("confPaperPercentWithCoAuthorHashMap", confPaperPercentWithCoAuthorHashMap);

            JournalBO jourBO = new JournalBO();
            HashMap jourPaperPercentWithCoAuthorHashMap = jourBO.calculateTotalJournalPercentWithCoAuthor(authorVertexIDList,
                                                                                                initialAuthorIDList);
            jourPaperPercentWithCoAuthorHashMap = HashMapUtility.getSortedMapDescending(jourPaperPercentWithCoAuthorHashMap);
            request.setAttribute("jourPaperPercentWithCoAuthorHashMap", jourPaperPercentWithCoAuthorHashMap);

            RequestDispatcher dispatcher = request.getRequestDispatcher("./jsp/MetricsTotalConfPercentWithCoAuthor.jsp");
            dispatcher.forward(request, response);
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
